package com.rxyb.sms.utils.bami;

import com.alibaba.fastjson.JSON;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.sms.constants.KeyouProperties;
import com.rxyb.sms.utils.SmsRspData;
import com.rxyb.sms.utils.keyou.HexUtil;
import com.rxyb.sms.utils.keyou.HttpUtil;
import com.rxyb.sms.utils.keyou.RandomUtil;
import com.rxyb.sms.utils.keyou.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信下发
 *
 * @coder duyh,@auth duyh
 * @date Nov 30, 2018
 */
@Slf4j
@Service
public class BamiClient {

    @Autowired
    private KeyouProperties keyouConstants;

    public SmsRspData send(String mobile, String message) {
        log.info("八米短信发送中。。。。。。");
        SmsRspData result = new SmsRspData();
        result.setMobileNo(mobile);
        result.setMessage(message);

        // 封装短信发送报文，包括签名验签加解密
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> hdata = new HashMap<>();
        // 公共参数
        data.put("agtId", keyouConstants.getAgtId());
        data.put("merId", keyouConstants.getMerId());
        data.put("nonceStr", RandomUtil.randomString(16));
        data.put("versionId", keyouConstants.getVersionId());

        // 需要转HEX的参数请使用HexUtil.str2Hex()方法
        // 业务参数,自行放入其他参数
        data.put("content", HexUtil.str2Hex(message));
        data.put("mobile", mobile);
        data.put("prodId", keyouConstants.getProdId());
        data.put("smsType", keyouConstants.getTypeVertify()); // 01 通知类 02 验证码类 03 营销类 04 营销特殊类

        String sign = HttpUtil.getSign(data, keyouConstants.getSignKey());
        try {
            sign = SecurityUtil.sign(sign, keyouConstants.getPrvkey(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        hdata.put("sign", sign);

        Map<String, Object> map = new HashMap<>();
        map.put("REQ_BODY", data);
        map.put("REQ_HEAD", hdata);

        String reqJson = JSON.toJSONString(map);
        log.info("请求地址：【{}】", keyouConstants.getPostUrl());
        result.setReqUrl(keyouConstants.getPostUrl());

        log.info("请求报文：【{}】", reqJson);
        result.setReqMsg(reqJson);

        String rspJson = HttpUtil.post(keyouConstants.getPostUrl(), reqJson);
        log.info("返回报文：【{}】", rspJson);
        result.setRspMsg(rspJson);

        try {
            Map<String, Object> rmap = JSON.parseObject(rspJson, Map.class);
            Map<String, Object> _body = (Map<String, Object>) rmap.get("REP_BODY");
            Map<String, Object> _head = (Map<String, Object>) rmap.get("REP_HEAD");
            if ("E00000".equals(_body.get("rspcode")) || "000000".equals(_body.get("rspcode"))) {
                String vsign = HttpUtil.getSign(_body, keyouConstants.getSignKey());
                String _sign = _head.get("sign").toString();
                boolean flag = SecurityUtil.verify(vsign, _sign, keyouConstants.getPubKey(), true);
                log.info("验证签名:【{}】", flag);
                result.setRspState(DbConstants.GLOBAL_STATE_SUCC);
                return result;
            } else {
                log.error("第三方返回错误原因:【{}】", HexUtil.hex2Str(_body.get("rspmsg").toString()));
            }
        } catch (Exception e) {
            log.error("短信发送异常", e);
            result.setRspMsg(rspJson + "==>" + e.getMessage());
        }
        result.setRspState(DbConstants.GLOBAL_STATE_FAILED);
        return result;
    }

}
